let allButtons = {
  fp: {
    fun: 'fenpei',
    type: 'primary',
    name: '分配'
  },
  rl: {
    fun: 'renling',
    type: 'primary',
    name: '抢单'
  },
  yj: {
    fun: 'yijiao',
    type: 'primary',
    name: '移交'
  },
  qs: {
    fun: 'qianshou',
    type: 'primary',
    name: '签收'
  },
  bl: {
    fun: 'banli',
    type: 'primary',
    name: '办理'
  },
  th: {
    fun: 'tuihui',
    name: '退回'
  },
  xq: {
    fun: 'xiangqing',
    name: '详情'
  },
}
import {
  Dialog
} from 'vant';
export default {
  data() {
    return {
      taskList: [],
      offset: 0,
      limit: 10,
      loadMore: true,
      pageParams: {},
      scrollHeight: 0,
      scrollerRef: 'myscroll',
      initSearch: true,
      backlogCount: 0,
      disposeData: {}
    }
  },
  created() {
    let that = this
    let divHeight = window.screen.availHeight;
    this.getHeight(divHeight)
    window.onresize = () => {
      setTimeout(() => {
        let divHeight = window.screen.availHeight;
        that.getHeight(divHeight)
      }, 500)
    }
  },
  methods: {
    getHeight(screenHeight) {
      if (this.$route.path == '/task/taskHandle') {
        this.scrollHeight = String(screenHeight - 158)
      } else {
        this.scrollHeight = String(screenHeight - 120)
      }
    },

    refresh(done) {
      //下拉刷新
      this.offset = 0;
      this.taskList = [];
      let params = this.pageParams
      params.searchParam.offset = 0
      this.getTaskList(params, done);
    },

    infinite(done) {
      this.$refs[this.scrollerRef].finishInfinite(false);
      //上拉加载
      if (this.loadMore) {
        this.offset = Number(this.offset) + Number(this.limit);
        this.getTaskList(this.pageParams, done);
      } else {
        this.$refs[this.scrollerRef].finishInfinite(true);
      }
    },

    getTaskList(params, done) {
      if (params.type == 'taskHandle1') {
        this.scrollerRef = 'myscroll1'
      } else {
        this.scrollerRef = 'myscroll'
      }
      let data = params.searchParam
      if (!params.searchParam.offset) {
        data.offset = this.offset
      }
      if (!params.searchParam.limit) {
        data.limit = this.limit
      }
      let UserInfo = localStorage.getItem('userMsg') ? JSON.parse(localStorage.getItem('userMsg')) : ''
      // let userMsg = localStorage.getItem('userMsg')
      if (UserInfo) {
        // let userInfo = JSON.parse(userMsg)
        if (UserInfo.currentOrg) {
          data.userGroup = UserInfo.currentOrg.groupId
        } else {
          this.$toast.fail('暂无权限')
          return
        }
      } else {
        this.$toast.fail('请先登录')
        return
      }
      this.$toast.loading({
        message: '加载中...',
        duration: 0
      })
      this.$refs[this.scrollerRef].finishPullToRefresh();
      let post = this.baseService.postForm(this.__ctx + '/taskPool/select', data)
      post.then(res => {
        this.initSearch = false
        if (res.isOk) {
          if (params.type == 'taskHandle0') {
            this.backlogCount = res.total
          }
          let that = this
          // setTimeout(() => {
          //   that.initSearch = false
          // },1000)
          this.$toast.clear()
          let list = res.rows
          if (list.length > 0) {
            if (list.length < data.limit) {
              this.loadMore = false
            }
            this.disposeTaskList(list, params.type)
          } else {
            this.$refs[this.scrollerRef].finishInfinite(true);
            this.loadMore = false
          }
        } else {
          this.$refs[this.scrollerRef].finishInfinite(true);
          this.$toast.fail(res.msg)
        }
        if (typeof done == 'function') {
          done()
        }
      })
      post.catch(err => {
        this.initSearch = false
        this.$refs[this.scrollerRef].finishInfinite(true);
        this.$toast.clear()
      })
    },

    nextPage(searchParam, type) {
      if (!this.loadMore) {
        return
      }
      this.offset += this.limit
      this.getTaskList(searchParam, type)
    },

    disposeTaskList(data, type) {
      let content = [{
          // width: '100%',
          key: 'createTime',
          title: '创建时间：',
          value: ''
        },
        {
          width: '100%',
          key: 'custType',
          title: '申请主体：',
          value: ''
        },
        {
          width: '50%',
          key: 'applicPeriods',
          title: '申请期限：',
          value: ''
        },
        {
          width: '50%',
          key: 'aplyAmt',
          title: '申请金额：',
          value: ''
        },
        {
          width: '50%',
          key: 'prodNam',
          title: '业务产品：',
          value: ''
        },
        {
          width: '50%',
          key: 'mainLonGrteTyp',
          title: '担保方式：',
          value: ''
        },
        {
          width: '50%',
          key: 'defName',
          title: '任务类型：',
          value: ''
        },
        {
          width: '50%',
          title: '紧迫性：',
          key: 'emergency',
          value: '',
          type: 'tag',
          priority: 0
        },
        {
          row: {},
          key: 'action',
          btnList: [allButtons.xq]
        }
      ]
      let UserInfo = localStorage.getItem('userMsg') ? JSON.parse(localStorage.getItem('userMsg')) : ''
      let arr = []
      data.forEach(v => {
        let arr1 = []
        content.forEach((k, kindex) => {
          let obj = {}
          if (k.key == 'action') {
            obj.row = v
            let btnList = [allButtons.xq]
            if (v.status == 0 && v.taskOrgLevel == 5) {
              if (UserInfo.buttonPermision.RL) {
                btnList.unshift(allButtons.rl)
              }
              if (UserInfo.buttonPermision.FP) {
                btnList.unshift(allButtons.fp)
              }
            }
            if (v.status == 1 && v.initiator == UserInfo.user.userId) {
              btnList.unshift(allButtons.qs, allButtons.th)
            }
            if (v.status == 1 && UserInfo.buttonPermision.YJ) {
              btnList.unshift(allButtons.yj)
            }
            if ((v.status == 2 || v.status == 4) && v.initiator == UserInfo.user.userId) {
              btnList.unshift(allButtons.bl)
            }
            obj.btnList = btnList
          } else {
            // console.log(type,kindex)
            if ((type == 'taskHandle0' || type == 'taskHandle1') && kindex == 0) {
              let colorArr = ['', '#3F7AD6', '#FF4A32', '#FF4A32', '#86909C', '#86909C']
              obj.statusDesc = v.statusName
              // console.log(v.statusName)
              obj.statusColor = colorArr[v.status]
              obj.value = v[k.key] ? v[k.key] : ''
            } else {
              obj.value = v[k.key] ? v[k.key] : ''
            }
          }
          arr1.push({
            ...k,
            ...obj
          })
        })
        arr.push(arr1)
      })
      this.taskList = this.taskList.concat(arr)
    },

    taskDispose(fun, row) {
      let UserInfo = localStorage.getItem('userMsg') ? JSON.parse(localStorage.getItem('userMsg')) : ''
      let message = ''
      let url = '/taskPool/update'
      let data = {
        id: row.id,
        initiator_: UserInfo.user.userId,
        version: row.version
      }
      let resultMsg = ''
      let errMsg = ''
      switch (fun) {
        case 'qianshou':
          message = `是否确认签收${row.custType || ''}的${row.defName || ''}任务`
          data.status = 2
          resultMsg = '签收成功'
          errMsg = '签收失败'
          this.openDialog({
            url,
            data,
            message,
            resultMsg,
            errMsg
          })
          break;
        case 'tuihui':
          message = `是否确认退回${row.custType || ''}的${row.defName || ''}任务`
          data.status = 0
          resultMsg = '拒绝成功'
          errMsg = '拒绝失败'
          this.openDialog({
            url,
            data,
            message,
            resultMsg,
            errMsg
          })
          break;
        case 'renling':
          message = `是否确认抢单${row.custType || ''}的${row.defName || ''}任务`
          data.status = 1
          resultMsg = '抢单成功'
          errMsg = '抢单失败'
          this.openDialog({
            url,
            data,
            message,
            resultMsg,
            errMsg
          })
          break;
        case 'xiangqing':
          this.$router.push({
            path: '/task/taskDetail',
            query: {
              taskId: row.taskId,
              defId: row.defId,
              instId: row.instId,
              id: row.id
            }
          })
          break;
        case 'banli':
          this.$router.push({
            path: '/task/taskForm',
            query: {
              taskId: row.taskId
            }
          })
          break;
        case 'fenpei':
          this.disposeData = row
          this.$refs.disposeTaskModal.openModal()
          break;
        case 'yijiao':
          this.disposeData = row
          this.$refs.disposeTaskModal.openModal()
          break;

      }

    },

    openDialog(data) {
      Dialog.confirm({
        title: '提示信息',
        message: data.message
      }).then(() => {
        this.$toast.loading({
          message: '加载中...',
          duration: 0
        })
        let post = this.baseService.post(this.__ctx + data.url, data.data)
        post.then((res) => {
          if (res.isOk) {
            this.$toast.success(data.resultMsg)
            let that = this
            setTimeout(() => {
              that.refresh()
            }, 2000)
          } else {
            this.$toast.fail(res.msg)
          }
        }).catch(err => {
          this.$toast.fail(data.errMsg)
        })
      })
    }
  }
}
